namespace LabChecker.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using LabChecker.Models;

    internal sealed class Configuration : DbMigrationsConfiguration<LabCheckerContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(LabCheckerContext context)
        {
            var dennis = new User { FirstName = "Den", LastName = "Emel" };
            var dennisLogin = new InternalLogin { User = dennis, Email = "den@emel.ru", PasswordHash = "emel" };

            AddUserWithLogin(context, dennis, dennisLogin);

            var g1120 = new Group { Name = "1120" };
            context.Groups.AddOrUpdate(g => g.Name, g1120);

            var g1121 = new Group { Name = "1121" };
            context.Groups.AddOrUpdate(g => g.Name, g1121);

            var petrov = new Student { FirstName = "Ivan", LastName = "Petrov", Group = g1120 };
            var petrovLogin = new InternalLogin { User = petrov, Email = "ivan@petrov.ru", PasswordHash = "petrov" };

            AddUserWithLogin(context, petrov, petrovLogin);

            //System.Diagnostics.Debugger.Launch();
            //System.Diagnostics.Debug.WriteLine(System.IO.Directory.GetCurrentDirectory());

            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }

        private static void AddUserWithLogin(LabCheckerContext context, User user, InternalLogin login)
        {
            context.Users.AddOrUpdate(u => u.FirstName, user);
            context.InternalLogins.AddOrUpdate(l => l.Email, login);
        }
    }
}
